package com.probox.modules.controller.wechat;


import cn.hutool.core.io.resource.ClassPathResource;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.entity.domain.wechat.PayAudit.WechatPayAudit;
import com.probox.common.entity.domain.wechat.PayAudit.additionInfo.AdditionInfo;
import com.probox.common.entity.domain.wechat.PayAudit.bankaccountinfo.BankAccountInfo;
import com.probox.common.entity.domain.wechat.PayAudit.businessinfo.BusinessInfo;
import com.probox.common.entity.domain.wechat.PayAudit.contactInfo.ContactInfo;
import com.probox.common.entity.domain.wechat.PayAudit.settlementinfo.SettlementInfo;
import com.probox.common.entity.domain.wechat.PayAudit.subject_info.SubjectInfo;
import com.probox.common.entity.domain.wechat.WechatImg;
import com.probox.common.entity.domain.wechat.vo.QuerySwitchPayAuditVo;
import com.probox.common.entity.service.wechat.impl.WechatPayAuditServiceImpl;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.*;
import java.util.List;


/**
 * 微信特约商户申请
 *
 * @author Kite
 * @date 2021/6/28
 */
@RestController
@RequestMapping("/wechat/pay/audit")
public class WechatPayAuditController extends BaseController {


    @Autowired
    private WechatPayAuditServiceImpl wechatPayAuditService;


    /**
     * 新增支付授权单
     */
    @ApiOperation(value = "新增支付授权单")
    @Log(title = "新增支付授权单", businessType = BusinessType.GRANT)
    @PostMapping("/addPayAudit/{authorizerAppid}")
    public AjaxResult addPayAudit(@RequestBody WechatPayAudit wechatPayAudit, @PathVariable("authorizerAppid") String authorizerAppid) {
        return AjaxResult.success(wechatPayAuditService.addPayAudit(wechatPayAudit,authorizerAppid));
    }

    /**
     * 查询支付授权单
     */
    @ApiOperation(value = "查询支付授权单")
    @Log(title = "查询支付授权单", businessType = BusinessType.GRANT)
    @GetMapping("/getPayAudit/{businessCode}")
    public AjaxResult getPayAudit(@PathVariable("businessCode") String businessCode) {
        return AjaxResult.success(wechatPayAuditService.getPayAudit(businessCode));
    }

    /**
     * 查询主体资料
     */
    @ApiOperation(value = "查询主体资料")
    @Log(title = "查询主体资料", businessType = BusinessType.GRANT)
    @GetMapping("/getSubjectInfo/{businessCode}")
    public AjaxResult getSubjectInfo(@PathVariable("businessCode") String businessCode){
        return AjaxResult.success(wechatPayAuditService.getSubjectInfo(businessCode));
    }

    /**
     * 修改主体信息
     */
    @ApiOperation(value = "修改主体类型")
    @Log(title = "修改主体类型", businessType = BusinessType.GRANT)
    @PostMapping("/updatePayAuditType/{businessCode}")
    public AjaxResult updatePayAuditType(@RequestBody WechatPayAudit wechatPayAudit,@PathVariable("businessCode") String businessCode){
        return AjaxResult.success(wechatPayAuditService.updatePayAuditType(wechatPayAudit,businessCode));
    }

    /**
     * 查询经营资料
     */
    @ApiOperation(value = "查询经营资料")
    @Log(title = "查询经营资料", businessType = BusinessType.GRANT)
    @GetMapping("/getBusinessInfo/{businessCode}")
    public AjaxResult getBusinessInfo(@PathVariable("businessCode") String businessCode){
        return AjaxResult.success(wechatPayAuditService.getBusinessInfo(businessCode));
    }

    /**
     * 查询银行账户
     */
    @ApiOperation(value = "查询银行账户")
    @Log(title = "查询银行账户", businessType = BusinessType.GRANT)
    @GetMapping("/getBankAccountInfo/{businessCode}")
    public AjaxResult getBankAccountInfo(@PathVariable("businessCode") String businessCode){
        return AjaxResult.success(wechatPayAuditService.getBankAccountInfo(businessCode));
    }

    /**
     * 查询超级管理员
     */
    @ApiOperation(value = "查询超级管理员")
    @Log(title = "查询超级管理员", businessType = BusinessType.GRANT)
    @GetMapping("/getContactInfo/{businessCode}")
    public AjaxResult getContactInfo(@PathVariable("businessCode") String businessCode){
        return AjaxResult.success(wechatPayAuditService.getContactInfo(businessCode));
    }

    /**
     * 查询结算规则
     */
    @ApiOperation(value = "查询结算规则")
    @Log(title = "查询结算规则", businessType = BusinessType.GRANT)
    @GetMapping("/getSettlementInfo/{businessCode}")
    public AjaxResult getSettlementInfo(@PathVariable("businessCode") String businessCode){
        return AjaxResult.success(wechatPayAuditService.getSettlementInfo(businessCode));
    }


    /**
     * 上传图片至微信
     */
    @ApiOperation(value = "上传图片至微信")
    @Log(title = "上传图片至微信", businessType = BusinessType.GRANT)
    @PostMapping("/uploadingImg")
    public AjaxResult uploadingImg(HttpServletRequest request) throws Exception {
        String qnyUrl = request.getParameter("qnyUrl");
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        MultipartFile file = null;
        if (files != null && files.size()>0){
            file = files.get(0);
            logger.info("收到{}个文件，文件名为：{}",files.size(),file.getName());
        }else{
            logger.info("没有收到文件");
        }
        File f = new File(file.getResource().getFilename());
        FileUtils.copyInputStreamToFile(file.getInputStream(), f);
        String img = wechatPayAuditService.uploadingImg(f,qnyUrl);
        return AjaxResult.success(img);
    }

    /**
     * 查询上传至微信的图片
     */
    @ApiOperation(value = "根据微信唯一标识查询图片")
    @Log(title = "根据微信唯一标识查询图片", businessType = BusinessType.GRANT)
    @GetMapping("/getWachatImg/{mediaIds}")
    public AjaxResult getWachatImg(@PathVariable("mediaIds") String[] mediaIds){
        List<WechatImg> img = wechatPayAuditService.getWachatImg(mediaIds);
        return AjaxResult.success(img);
    }


    /**
     * 新增超级管理员信息
     */
    @ApiOperation(value = "新增超级管理员信息")
    @Log(title = "新增超级管理员信息", businessType = BusinessType.GRANT)
    @PostMapping("/addContactInfo/{businessCode}")
    public AjaxResult addContactInfo(@Valid @RequestBody ContactInfo contactInfo,@PathVariable("businessCode") String businessCode) {
        return AjaxResult.success(wechatPayAuditService.addContactInfo(contactInfo,businessCode));
    }

    /**
     * 新增主体资料
     */
    @ApiOperation(value = "新增主体资料")
    @Log(title = "新增主体资料", businessType = BusinessType.GRANT)
    @PostMapping("/addSubjectInfo/{businessCode}")
    public AjaxResult addSubjectInfo(@RequestBody SubjectInfo subjectInfo,@PathVariable("businessCode") String businessCode) {
        return AjaxResult.success(wechatPayAuditService.addSubjectInfo(subjectInfo,businessCode));
    }

    /**
     * 新增经营资料
     */
    @ApiOperation(value = "新增经营资料")
    @Log(title = "新增经营资料", businessType = BusinessType.GRANT)
    @PostMapping("/addBusinessInfo/{businessCode}")
    public AjaxResult addBusinessInfo(@RequestBody BusinessInfo businessInfo,@PathVariable("businessCode") String businessCode) {
        return AjaxResult.success(wechatPayAuditService.addBusinessInfo(businessInfo,businessCode));
    }


    /**
     * 新增结算规则
     */
    @ApiOperation(value = "新增结算规则")
    @Log(title = "新增结算规则", businessType = BusinessType.GRANT)
    @PostMapping("/addSettlementInfo/{businessCode}")
    public AjaxResult addSettlementInfo(@Valid @RequestBody SettlementInfo settlementInfo,@PathVariable("businessCode") String businessCode) {
        return AjaxResult.success(wechatPayAuditService.addSettlementInfo(settlementInfo,businessCode));
    }

    /**
     * 新增结算银行账户
     */
    @ApiOperation(value = "新增结算银行账户")
    @Log(title = "新增结算银行账户", businessType = BusinessType.GRANT)
    @PostMapping("/addBankAccountInfo/{businessCode}")
    public AjaxResult addBankAccountInfo(@Valid @RequestBody BankAccountInfo bankAccountInfo,@PathVariable("businessCode") String businessCode) {
        return AjaxResult.success(wechatPayAuditService.addBankAccountInfo(bankAccountInfo,businessCode));
    }


    /**
     * 新增补充材料
     */
    @ApiOperation(value = "新增补充材料")
    @Log(title = "新增补充材料", businessType = BusinessType.GRANT)
    @PostMapping("/addAdditionInfo/{businessCode}")
    public AjaxResult addAdditionInfo(@RequestBody AdditionInfo additionInfo,@PathVariable("businessCode") String businessCode) {
        return AjaxResult.success(wechatPayAuditService.addAdditionInfo(additionInfo,businessCode));
    }

    /**
     * 提交审核
     */
    @ApiOperation(value = "提交审核")
    @Log(title = "提交审核", businessType = BusinessType.GRANT)
    @PostMapping("/submitAudit/{businessCode}")
    public AjaxResult submitAudit(@PathVariable("businessCode") String businessCode) throws Exception {
        return AjaxResult.success(wechatPayAuditService.submitAudit(businessCode));
    }

    /**
     * 查询地区信息
     */
    @ApiOperation(value = "查询地区信息")
    @Log(title = "查询地区信息", businessType = BusinessType.GRANT)
    @GetMapping("/getWechatArea")
    public AjaxResult getWechatArea(String addCitycode) {
        return AjaxResult.success(wechatPayAuditService.getWechatArea(addCitycode));
    }

    /**
     * 查询上级地区信息
     */
    @ApiOperation(value = "查询上级地区信息")
    @Log(title = "查询上级地区信息", businessType = BusinessType.GRANT)
    @GetMapping("/getAddCitycode")
    public AjaxResult getAddCitycode(String addCitycode) {
        return AjaxResult.success(wechatPayAuditService.getAddCitycode(addCitycode));
    }

//    /**
//     * 查询申请单状态
//     */
//    @ApiOperation(value = "查询申请单状态")
//    @Log(title = "查询申请单状态", businessType = BusinessType.GRANT)
//    @GetMapping("/getApplyState")
//    public AjaxResult getApplyState() {
//        return AjaxResult.success(wechatPayAuditService.getApplyState());
//    }


//    /**
//     * 查询申请单状态
//     */
//    @ApiOperation(value = "查询申请单状态")
//    @Log(title = "查询申请单状态", businessType = BusinessType.GRANT)
//    @GetMapping("/getPayAuditStatus")
//    public AjaxResult getPayAuditStatus() {
//        return AjaxResult.success(wechatPayAuditService.getPayAuditStatus());
//    }

    /**
     * 查询商户信息
     */
    @ApiOperation(value = "查询申请单状态")
    @Log(title = "查询申请单状态", businessType = BusinessType.GRANT)
    @GetMapping("/getCommercialData/{businessCode}")
    public AjaxResult getCommercialData(@PathVariable("businessCode")String businessCode) {
        return AjaxResult.success(wechatPayAuditService.getCommercialData(businessCode));
    }

    /**
     * 查询小程序/公众号ID
     */
    @ApiOperation(value = "查询小程序/公众号ID")
    @Log(title = "查询小程序/公众号ID", businessType = BusinessType.GRANT)
    @GetMapping("/getAppId")
    public AjaxResult getAppId() {
        return AjaxResult.success(wechatPayAuditService.getAppId());
    }


    /**
     * 查询授权支付列表
     */
    @ApiOperation(value = "查询授权支付列表")
    @Log(title = "查询授权支付列表", businessType = BusinessType.GRANT)
    @GetMapping("/wechatPayAuditList/{type}")
    public AjaxResult wechatPayAuditList(@PathVariable("type") String type){
        return AjaxResult.success(wechatPayAuditService.wechatPayAuditList(type));
    }


    /**
     * 切换授权
     */
    @ApiOperation(value = "切换授权")
    @Log(title = "切换授权", businessType = BusinessType.GRANT)
    @PostMapping("/switchPayAudit")
    public AjaxResult switchPayAudit(@RequestBody QuerySwitchPayAuditVo vo){
        return AjaxResult.success(wechatPayAuditService.switchPayAudit(vo));
    }
    @PostMapping("/download")
    public HttpServletResponse download(HttpServletResponse response) {
        try {
//            String path = this.getClass().getClassLoader().getResource("").getPath();//注意getResource("")里面是空字符串
//            String filePath = path + "opening.xlsx";

            ClassPathResource resource = new ClassPathResource("opening.xlsx");
            String filePath = resource.getUrl().getPath();

            // path是指欲下载的文件的路径。
            File file = new File(filePath);
            // 取得文件名。
            String filename = file.getName();
            // 取得文件的后缀名。
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return response;
    }

}
